// 注册页面设计

// 关闭VSCode非必要报错
// ignore_for_file: camel_case_types, use_build_context_synchronously, non_constant_identifier_names

import 'package:flutter/material.dart';
import 'package:project_award_statistics/utils/my_yaru_buttons/my_yaru_button_confirmed.dart';
import 'package:project_award_statistics/utils/dialog_yaru_design/register/dialog_register_err_empty.dart';
import 'package:project_award_statistics/utils/dialog_yaru_design/register/dialog_register_err_exists.dart';
import 'package:project_award_statistics/utils/dialog_yaru_design/register/dialog_register_success.dart';
import 'package:project_award_statistics/utils/load_user_info_from_db/load_user_info.dart';
import 'package:project_award_statistics/utils/load_user_info_from_db/user_model.dart';
import 'package:window_manager/window_manager.dart';
import 'package:yaru/widgets.dart';

class RegisterPage_Desktop extends StatelessWidget {
  const RegisterPage_Desktop({super.key});
  @override
  Widget build(BuildContext context) {
    // 更改窗口标题文字
    WindowManager.instance.setTitle("个人获奖信息查询  --   注册页面");

    // 定义文本输入控制器
    var textcontroller_user_name=TextEditingController();  // 输入用户名的
    var textcontroller_password=TextEditingController();  // 输入密码的

    void do_register (BuildContext context) async   //"注册"按钮触发的进行注册函数
      {
        //先载入用户信息数据库
        //先定义UserInfo类对象user_info_list
        List <user_info_model> user_info_list = await DBProvider_UserInfo().get_user_list();
        int i=0;
        //register_user_exists变量用于判断用户注册的用户名是否已经存在
        bool register_user_exists=false; 
        //register_user用于获取用户注册时输入的用户名
        String register_user=textcontroller_user_name.text; 

        //遍历数据库返回的列表进行查询用户目标注册对象是否已经存在,方便后期报错
        for (i=0;i<user_info_list.length;i++)
          {
            if (user_info_list[i].stu_name==register_user)
              {
                register_user_exists=true;
              }
          }

        if (textcontroller_user_name.text.isEmpty || textcontroller_password.text.isEmpty)   //如果检测到用户未输入注册的姓名/密码,那么进行对应报错处理
          { 
            showDialog(  //显示报错对话框
              context: context, 
              builder: (context){
                return Dialog_RegisterErrEmpty_Desktop();
              }
            );
          }
        else if (register_user_exists)  //如果用户重复注册
          {
            showDialog(   //显示用户重复注册报错的对话框
              context: context, 
              builder: (context){
                return DialogRegisterErr_Exists_Desktop();
              },
            );
          }
        else
          {
            user_info_model register_user_info=user_info_model(
              stu_name: textcontroller_user_name.text, 
              password: textcontroller_password.text,
            );
            DBProvider_UserInfo database=DBProvider_UserInfo();
            await database.insert_new_user(register_user_info);

            //显示注册成功对话框
            showDialog(
              context: context,
              builder: (context){
                return DialogRegisterSuccess_Desktop(stu_name: textcontroller_user_name.text,);
              },
            );
          }
      }

    return Scaffold(
      backgroundColor: Colors.white,
      
      body: Stack(
        children: [
          Positioned(  // 设置左上角的返回按钮
            top: 25,
            left: 25,
            child: SizedBox(
              width: 50,
              height: 50,
              child: YaruBackButton(   // 设置Yaru主题的返回按钮
                onPressed: () {
                  Navigator.of(context).pop();   // 返回上个页面
                  WindowManager.instance.setTitle("个人获奖信息查询  --   登录页面");    // 并且将窗口名称改回上个(登录)页面的
                },
              ),
            ),
          ),
          Padding(     // 先设置总控件之间的距离
            padding: EdgeInsets.only(left: 40,right: 60),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text(
                      "欢迎,新同学",
                      style: TextStyle(
                        fontSize: 50,
                      ),
                    ),
                    const SizedBox(height: 30,),
                    Text(
                      "请在右侧输入您的姓名与您想使用的密码",
                      style: TextStyle(
                        fontSize: 25,
                      ),
                    ),
                  ],
                ),
                Column(   // 列式布局放置用户与密码输入框
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    SizedBox(
                      width: 350,
                      child: TextField(
                        controller: textcontroller_user_name,
                        decoration: InputDecoration(
                          border: OutlineInputBorder(),
                          hintText: " 请在这里输入您的姓名",
                        ),
                      ),
                    ),
                    const SizedBox(height: 30,),   // 调整两控件间距离
                    SizedBox(
                      width: 350,
                      child: TextField(
                        controller: textcontroller_password,
                        obscureText: true, // 设置为true以隐藏输入的文本
                        decoration: InputDecoration(
                          border: OutlineInputBorder(),
                          hintText: " 请在这里输入您想使用的密码",
                        ),
                      ),
                    ),
                    const SizedBox(height: 50,),   // 调整两控件间距离
                    Row(    // 列式布局放置两按钮
                      children: [
                        SizedBox(
                          height: 40,
                          width: 200,
                          child: MyYaruButton_Confirmed(
                            text: "好的,我填写完了", 
                            onPressed:() async {
                              do_register(context);
                            },
                          ),
                        ),
                      ],
                    ),
                  ],
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
